//
//  CustomBaseCell.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/9.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

//protocol
protocol CustomBaseCellDelegate : class{
    
    func customCell(cell : CustomBaseCell?,event : AnyObject?)
    
}

class CustomBaseCell: UITableViewCell {
    
    weak var delegte     : CustomBaseCellDelegate?
    weak var dataAdapter : CellAdapter?
    weak var cellData    : AnyObject?
    var      indexPath   : NSIndexPath?
    weak var tableView   : UITableView?
    weak var controller  : UIViewController?
    var      disPlay     : Bool?
    
    internal class func getInstance(reuseIdentifier: String?) -> Self {
        
        return self.init(style: .Default, reuseIdentifier: reuseIdentifier)
    }
    
    required override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupCell()
        
        cellBuildSubViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        setupCell()
        
        cellBuildSubViews()
    }
    
    func selectedEvent() {}
}


// 更新cell 数据
extension CustomBaseCell {
    
    override func update(data: AnyObject?) {
        super.update(data)
        
        textLabel?.text = data as? String
        
    }
}

// cell 工厂
extension CustomBaseCell {
    
    // MARK: cell 工厂
    internal class func tableViewCell(cellType: CMDBCellType?,indexPath: NSIndexPath?) -> AnyObject? {
        
        switch cellType! {
            
        case .Default:
            return CustomBaseCell.getInstance("\(cellType!.rawValue)")
            
        default:
            return nil
        }
    }
}


//MARK : class func 
extension CustomBaseCell {
    
    /**
     Calculate the cell's from data, override by subclass
     
     - parameter data: Data.
     
     - returns: Cell's height.

     */
    
    class func HeightWithData(data : AnyObject) -> CGFloat { return 0 }
    
    class func Adapter(reuseIdentifier : String? = nil, data : AnyObject? = nil,cellHeight: CGFloat? = 0, type: Int? = nil) -> CellAdapter{
        
        let identifier = (reuseIdentifier == nil ? String(self.classForCoder) : reuseIdentifier)
        
        return CellAdapter.init(cellReuseIdentifier: identifier, data: data, cellHeight: cellHeight, cellType : type)
    }
    
    func updateWithNewCellHeight(height : CGFloat, animated : Bool = true) {
        
        guard tableView != nil && dataAdapter != nil else {
            
            return
        }
        
        if animated {
            
            dataAdapter?.cellHeight = height
            tableView?.beginUpdates()
            tableView?.endUpdates()
            
        } else {
            
            dataAdapter?.cellHeight = height
            tableView?.reloadData()
        }
    }
}


//MARK : overide by subClass
extension CustomBaseCell {

    func setupCell(){
        
        self.selectionStyle = .None
        
    }
    
    func cellBuildSubViews(){
        
        
    }
    
    func loadContent(){
        
    }
    
}
